import { getOrderSparePageSelect } from '@/api/order'
import { getBeforeCheckOrderSpareDetail } from '@/api/parts'
import { ref } from 'vue'

/**
 * 获取工单配件下拉选项列表
 */
export const useGetOrderSparePartOptions = () => {
  const getSparePartOptionsLoading = ref(false)
  const getSparePartOptionsParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })
  const sparePartOptions = ref<Array<OrderSparePartAPIResponse.SparePartOption>>([])

  const getSparePartOptions = async (orderId: string) => {
    try {
      getSparePartOptionsLoading.value = true
      const res = await getOrderSparePageSelect(getSparePartOptionsParam.value, orderId)
      getSparePartOptionsLoading.value = false
      if (res.data.code === 20000) {
        sparePartOptions.value = res.data.data.rows
        return ''
      }
      return res.data.message
    } catch (error) {
      return '获取配件列表失败'
    }
  }

  return {
    sparePartOptions,
    getSparePartOptionsParam,
    getSparePartOptionsLoading,
    getSparePartOptions,
  }
}

/**
 * 获取配件审核之前的信息
 */
export const useGetOrderSparePartInfoBeforeCheck = () => {
  // 获取信息加载
  const getInfoLoading = ref(false)

  // 审核之前的工单配件单信息
  const info = ref<OrderSparePartInfoBeforeAudit>({
    applyEmp: '',
    applyRemark: '',
    createTime: '',
    id: '',
    oldCopeWay: 0,
    receiptAddrType: 2,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    code: '',
    applyEmpType: 1,
    networkSpareType: 1,
    received: false,
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: '',
    status: 1,
    type: 1,
    updateTime: '',
    userOrNetworkId: '',
    workId: '',
    workNo: '',
  })

  /**
   * 获取信息
   * @param id - 工单配件单id
   */
  const getInfo = async (id: string) => {
    try {
      getInfoLoading.value = true
      const res = await getBeforeCheckOrderSpareDetail(id)
      info.value = res.data.data.row
      getInfoLoading.value = false
    } catch (error) {
      getInfoLoading.value = false
    }
  }

  return {
    info,
    getInfoLoading,
    getInfo,
  }
}
